import Vue from 'vue'
import VueRouter from 'vue-router'
const Login = () => import(/* webpackChunkName: "login_home_welcome" */ '@/components/Login.vue');
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ '@/components/Home.vue');
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ '@/components/Welcome.vue');
const Users = () => import(/* webpackChunkName: "users" */ '@/components/user/Users.vue');
const Rights = () => import(/* webpackChunkName: "power" */ '@/components/power/Rights.vue');
const Roles = () => import(/* webpackChunkName: "power" */ '@/components/power/Roles.vue');
const Categories = () => import(/* webpackChunkName: "goods" */ '@/components/goods/Categories.vue');
const GoodsParams = () => import(/* webpackChunkName: "goods" */ '@/components/goods/GoodsParams.vue');
const GoodsList = () => import(/* webpackChunkName: "goods" */ '@/components/goods/GoodsList.vue');
const AddGoods = () => import(/* webpackChunkName: "goods" */ '@/components/goods/AddGoods.vue');
const EditGoods = () => import(/* webpackChunkName: "goods" */ '@/components/goods/EditGoods.vue');
const OrdersList = () => import(/* webpackChunkName: "orders" */ '@/components/orders/OrdersList.vue');
const Reports = () => import(/* webpackChunkName: "reports" */ '@/components/reports/Reports.vue');

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    redirect: '/home'
  },{
    path: '/login',
    name: 'Login',
    component: Login
  },{
    path: '/home',
    name: 'Home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        name: 'Welcome',
        component: Welcome,
      },
      {
        path: '/users',
        name: 'Users',
        component: Users,
      },
      {
        path: '/rights',
        name: 'Rights',
        component: Rights,
      },
      {
        path: '/roles',
        name: 'Roles',
        component: Roles,
      },
      {
        path: '/categories',
        name: 'Categories',
        component: Categories,
      },
      {
        path: '/params',
        name: 'GoodsParams',
        component: GoodsParams,
      },
      {
        path: '/goods',
        name: 'GoodsList',
        component: GoodsList,
      },
      {
        path: '/goods/add',
        name: 'AddGoods',
        component: AddGoods,
      },
      {
        path: '/goods/edit/:id',
        name: 'EditGoods',
        component: EditGoods,
      },
      {
        path: '/orders',
        name: 'OrdersList',
        component: OrdersList,
      },
      {
        path: '/reports',
        name: 'Reports',
        component: Reports,
      }
    ]
  }
]

const router = new VueRouter({
  routes
});

//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数表示放行  next()放行、next('/login')强制跳转
  // 如果用户访问的登录页，直接放行
  if (to.path === '/login') return next();
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token');
  // 没有token，强制跳转到登录页
  if(!tokenStr) return next('/login');
  next();
})

export default router
